<template>
  <div>
    <div class="header-container">
      <div class="width-container header-width-container">
        <div class="course-name-text">{{ courseInfo.courseName }}</div>
        <div class="teacher-course-info-container">
          <div class="teacher-info-container">
            <img :src="courseInfo.timg" alt="" />
            <div class="teacher-name-pos">
              <span>{{ courseInfo.tname }}</span>
              <span>{{ courseInfo.position }}</span>
            </div>
          </div>
          <div class="course-info-container">
            <span>难度 {{ courseInfo.level }}</span>
            <span>·</span>
            <span>学习人数 {{ courseInfo.enroll }}</span>
            <span>·</span>
            <span>综合评分 {{ courseInfo.rate }}</span>
          </div>
          <div class="collect-by">
            <div class="course-value">
              <span class="value-span" v-if="courseInfo.free == '0' && !hadFlag"
                >￥{{ courseInfo.price }}</span
              >
              <span
                v-if="courseInfo.free == '0' && !hadFlag"
                class="add-shopping-cart"
                @click.stop="addToShopping(courseInfo.id)"
                >加购物车</span
              >
              <span
                v-if="collectList.includes(courseInfo.id)"
                class="collect-course-star collect-course"
                @click.stop="setCollect(courseInfo.id, 1)"
                >已收藏</span
              >
              <span
                v-else
                class="collect-course-star"
                @click.stop="setCollect(courseInfo.id, 0)"
                >收藏</span
              >
            </div>
          </div>
        </div>
      </div>
    </div>

    <div class="tabs-container">
      <div class="width-container tabs-width-container">
        <div
          @click="activeTagIndex = 0"
          :class="['tabs-block', { 'active-tab': activeTagIndex == 0 }]"
        >
          章节
        </div>
        <div
          @click="activeTagIndex = 1"
          :class="['tabs-block', { 'active-tab': activeTagIndex == 1 }]"
        >
          评价
        </div>
      </div>
    </div>

    <div class="main-container">
      <div class="width-container">
        <div v-if="activeTagIndex == 0" class="course-list-container">
          <el-card>
            <div slot="header" class="clearfix course-card-header">
              <b> 课程描述 </b>
            </div>
            <div v-html="courseInfo.description"></div>
          </el-card>
          <el-card v-for="(item, index) in courseChapter" :key="index">
            <div slot="header" class="clearfix course-card-header">
              <div class="chapterTitle">{{ item.name }}</div>
              <div class="chapterDescription">
                {{ item.description }}
              </div>
            </div>
            <div class="section-container">
              <div
                v-for="(node, step) in item.sectionList"
                :key="step"
                class="section-block"
                @click="toVideo(node.id)"
              >
                <i class="el-icon-video-play"></i>
                <span>{{ `${index + 1}-${step + 1} ${node.title}` }}</span>
              </div>
            </div>
          </el-card>
        </div>
        <div v-else class="course-comment-list-container">
          <div class="send-comment-wrap">
            <div>课程评价</div>
            <div class="send-comment-button" @click="dialogTableVisible = true">
              发布评论
            </div>
          </div>
          <el-card v-for="(item, index) in commentList" :key="index">
            <div class="comment-user-block">
              <img :src="item.userImg" alt="" />
              <div class="comment-user-name">{{ item.nickName }}</div>
            </div>
            <div class="comment-content">{{ item.comment }}</div>
            <div class="comment-time">
              <el-rate
                v-model="item.rate"
                disabled
                show-score
                text-color="#ff9900"
              >
              </el-rate>
              <span> 时间：{{ item.createTime.split("T")[0] }} </span>
            </div>
          </el-card>
        </div>
        <div class="adv-list-container">
          <el-card>
            <div slot="header" class="clearfix adv-title">推荐课程</div>
            <div class="adv-course-list">
              <div
                v-for="(item, index) in advList"
                :key="index"
                class="course-container"
                @click="
                  $myUtils.OpenNewWindow('/courseDetail', { id: item.id })
                "
              >
                <img :src="item.image" alt="" />
                <div class="course-info-block">
                  <div class="course-name">{{ item.courseName }}</div>
                  <div class="value-applay">
                    <span>￥{{ item.price }}</span>
                    <span>·</span>
                    <span>{{ item.level }}</span>
                    <span>·</span>
                    <span>{{ item.enroll }}</span>
                  </div>
                </div>
              </div>
            </div>
          </el-card>
        </div>
      </div>
    </div>

    <el-dialog
      title="我要评论"
      :visible.sync="dialogTableVisible"
      @close="
        dialogTableVisible = false;
        myComment = '';
      "
    >
      <el-input
        type="textarea"
        resize="none"
        :rows="4"
        placeholder="扯淡、吐槽、表扬、鼓励……想说啥就说啥！"
        v-model="myComment"
      >
      </el-input>
      <div class="button-group">
        <div class="my-rate">
          <span>评分：</span>
          <el-rate v-model="myRate"> </el-rate>
        </div>
        <el-button @click="sendComment">发表评论</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  getCourseInfo,
  getCourseChapter,
  getCourseComment,
  toCommentCourse,
  getRecommendList,
} from "@/services/course";
import {
  getUserCollectCourseId,
  collectCourse,
  addCartCourse,
  getCartCourse,
  getUserCourseList,
  learnCourse,
} from "@/services/user";

export default {
  data() {
    return {
      courseId: "",
      courseInfo: [],
      courseChapter: [],
      advList: [],
      activeTagIndex: 0,
      commentList: [],
      dialogTableVisible: false,
      myComment: "",
      myRate: 5,
      cartCourse: [],
      hadCourseList: [],
      collectList: [],
      hadFlag: true,
      inCartFlag: false,
    };
  },
  methods: {
    toGetCourseInfo(courseId) {
      getCourseInfo({ courseId }).then((res) => {
        this.courseInfo = res.data.data;
        if (this.courseInfo.free && !this.hadFlag) {
          learnCourse({ courseId: this.courseId });
        }
      });
    },
    toGetCourseChapter(courseId) {
      getCourseChapter({ courseId }).then((res) => {
        this.courseChapter = res.data.data;
      });
    },
    getAdvList() {
      getRecommendList({ number: 8 }).then((res) => {
        this.advList = res.data.data;
      });
    },
    getCourseCommentList(courseId) {
      getCourseComment({ courseId }).then((res) => {
        this.commentList = res.data.data;
      });
    },
    sendComment() {
      toCommentCourse({
        courseId: this.courseId,
        comment: this.myComment,
        rate: this.myRate,
      }).then((res) => {
        this.myComment = "";
        this.dialogTableVisible = false;
        this.getCourseCommentList(this.courseId);
      });
    },
    toVideo(sectionId) {
      if (this.courseInfo.free || this.hadFlag) {
        this.$router.push({
          path: "/videoDetail",
          query: {
            courseId: this.courseId,
            sectionId,
          },
        });
      } else {
        this.$confirm("", "您未购买此课程，是否购买？", {
          confirmButtonText: "加入购物车",
          cancelButtonText: "取消",
          type: "warning",
          center: true,
        })
          .then(() => {
            this.addToShopping(this.courseId);
          })
          .catch(() => {});
      }
    },
    checkCourse(courseId) {
      this.hadFlag = this.hadCourseList.some((item) => item.id == courseId);
      this.inCartFlag = this.cartCourse.some((item) => item.id == courseId);
      if (this.courseInfo.free && !this.hadFlag) {
        learnCourse({ courseId: this.courseId });
      }
    },
    addToShopping(courseId) {
      if (this.inCartFlag) {
        this.$confirm("", "课程已经在购物车中", {
          confirmButtonText: "去购物车结算",
          cancelButtonText: "继续逛逛",
          type: "success",
          iconClass: "el-icon-success",
          center: true,
        })
          .then(() => {
            this.$myUtils.OpenNewWindow("/shoppingCart/cart", {});
          })
          .catch(() => {});
      } else {
        addCartCourse({ courseId }).then((res) => {
          this.getShoppingCourse();
          this.checkCourse(courseId);
          this.$confirm("", "添加购物车成功", {
            confirmButtonText: "去购物车结算",
            cancelButtonText: "继续逛逛",
            type: "success",
            iconClass: "el-icon-success",
            center: true,
          })
            .then(() => {
              this.$myUtils.OpenNewWindow("/shoppingCart/cart", {});
            })
            .catch(() => {});
        });
      }
    },
    getShoppingCourse() {
      getCartCourse().then((res) => {
        this.cartCourse = res.data.data || [];
        this.inCartFlag = this.cartCourse.some(
          (item) => item.id == this.courseId
        );
      });
    },
    getHadCourseList() {
      getUserCourseList().then((res) => {
        this.hadCourseList = res.data.data || [];
        this.hadFlag = this.hadCourseList.some(
          (item) => item.id == this.courseId
        );
      });
    },
    getCollectList() {
      // console.log(getUserCollectCourseId);
      getUserCollectCourseId().then((res) => {
        this.collectList = res.data.data || [];
      });
    },
    setCollect(courseId, status) {
      collectCourse({ courseId, status }).then((res) => {
        this.$message(res.data.msg);
        this.getCollectList();
      });
    },
  },
  created() {
    this.courseId = this.$route.query.id;
    this.getShoppingCourse();
    this.getHadCourseList();
    this.getCollectList();
    this.getAdvList();
    this.toGetCourseInfo(this.courseId);
    this.toGetCourseChapter(this.courseId);
    this.getCourseCommentList(this.courseId);
  },
  watch: {
    "$route.query.id": function () {
      this.courseId = this.$route.query.id;
      this.getShoppingCourse();
      this.getHadCourseList();
      this.getCollectList();
      this.getAdvList();
      this.toGetCourseInfo(this.courseId);
      this.toGetCourseChapter(this.courseId);
      this.getCourseCommentList(this.courseId);
    },
  },
};
</script>
<style lang="scss" scoped>
.header-container {
  height: 168px;
  background-image: linear-gradient(
    to right,
    rgba(6, 43, 43, 1),
    rgba(13, 96, 188, 1)
  );
  .width-container.header-width-container {
    display: flex;
    flex-direction: column;
    .course-name-text {
      margin-top: 32px;
      margin-bottom: 8px;
      line-height: 48px;
      font-size: 32px;
      color: #fff;
    }
    .teacher-course-info-container {
      display: flex;
      align-items: center;
      .teacher-info-container {
        display: flex;
        margin-right: 60px;
        img {
          width: 48px;
          height: 48px;
          border-radius: 50%;
          margin-right: 8px;
        }
        .teacher-name-pos {
          display: flex;
          flex-direction: column;
          justify-content: space-around;
          span {
            font-size: 12px;
            color: #fff;
            &:first-child {
              font-size: 14px;
              margin-top: 3px;
              font-weight: bold;
            }
          }
        }
      }
      .course-info-container {
        display: flex;
        span {
          margin-right: 15px;
          font-size: 12px;
          color: white;
        }
      }
      .collect-by {
        flex: 1;
        display: flex;
        justify-content: flex-end;
        .course-value {
          font-size: 12px;
          display: flex;
          color: $primary-color;
          .value-span {
            font-weight: bold;
            margin-right: 20px;
          }
          .add-shopping-cart {
            margin-right: 50px;
            cursor: pointer;
            position: relative;
            &::before {
              content: "";
              display: block;
              position: absolute;
              transform: translate(-110%, 10%);
              background: url("~@/assets/icon/courseSelect/购物车.png") 0 0
                no-repeat;
              width: 12px;
              height: 12px;
            }
          }
          .collect-course-star {
            cursor: pointer;
            color: white;
            width: 50px;
            position: relative;
            &::before {
              content: "";
              display: block;
              position: absolute;
              transform: translate(-110%, 10%);
              background: url("~@/assets/icon/courseSelect/收藏.png") 0 0
                no-repeat;
              width: 14px;
              height: 14px;
            }
          }
          .collect-course {
            cursor: pointer;
            width: 50px;
            color: $primary-color;
            &::before {
              background: url("~@/assets/icon/courseSelect/已收藏.png") 0 0
                no-repeat;
            }
          }
        }
      }
    }
  }
}
.tabs-container {
  height: 68px;
  line-height: 68px;
  background: #fff;
  box-shadow: 0 4px 8px 0 rgb(28 31 33 / 10%);
  .width-container.tabs-width-container {
    width: 1152px;
    margin: 0 auto;
    display: flex;
    justify-content: flex-start;
    .tabs-block {
      position: relative;
      color: #1c1f21;
      font-weight: bold;
      margin-right: 60px;
      margin-top: 15px;
      line-height: 37px;
      cursor: pointer;
    }
    .active-tab {
      &::after {
        content: "";
        display: block;
        width: 20px;
        border-radius: 2px;
        height: 3px;
        background: #f20d0d;
        margin: 0 auto;
      }
    }
  }
}

.main-container {
  margin-top: 20px;
  .width-container {
    display: flex;
    justify-content: space-between;
    .course-list-container {
      flex: 1;
      .el-card {
        margin-bottom: 20px;
        .chapterTitle {
          color: #1c1f21;
          font-size: 16px;
          font-weight: 700;
          line-height: 24px;
        }
        .chapterDescription {
          margin-top: 2px;
          font-size: 12px;
          color: #545c63;
          line-height: 18px;
        }
        .section-container {
          .section-block {
            height: 48px;
            font-size: 14px;
            display: flex;
            justify-content: flex-start;
            align-items: center;
            cursor: pointer;
            i {
              font-size: 24px;
              color: #9199a1;
              margin-right: 4px;
              margin-left: 12px;
            }
            span {
              color: #1c1f21;
            }
            &:hover {
              background: rgba(242, 13, 13, 0.05);
              border-radius: 4px;
              color: #f20d0d;
              i,
              span {
                color: #f20d0d;
              }
            }
          }
        }
      }
    }
    .course-comment-list-container {
      flex: 1;
      .send-comment-wrap {
        display: flex;
        justify-content: space-between;
        margin-bottom: 20px;
        .send-comment-button {
          color: #545c63;
          background-color: rgba(84, 92, 99, 0.1);
          cursor: pointer;
          border-radius: 18px;
          padding: 7px 16px;
          font-size: 14px;
        }
      }
      ::v-deep .el-card {
        margin-bottom: 20px;
        .el-card__body {
          display: flex;
          padding-left: 10px;
          .comment-user-block {
            display: flex;
            flex-direction: column;
            align-items: center;
            margin-right: 20px;
            width: 80px;
            color: #787d82;
            font-size: 12px;

            img {
              width: 48px;
              height: 48px;
              margin-bottom: 10px;
            }
          }
          .comment-content {
            flex: 1;
            font-size: 14px;
            margin-top: 10px;
          }
          .comment-time {
            margin-top: 10px;
            width: 150px;
            font-size: 12px;
            color: #b5b9bc;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
            > span {
              margin-left: 4px;
            }
          }
        }
      }
    }
    .adv-list-container {
      width: 320px;
      padding-left: 32px;
      ::v-deep .el-card {
        background-color: transparent;
        border: 0;
        box-shadow: 0 0 0;
        .el-card__header {
          padding: 0;
          .adv-title {
            font-weight: 700;
            font-size: 14px;
            padding-bottom: 10px;
          }
        }
        .el-card__body {
          padding: 0;
          .adv-course-list {
            .course-container {
              display: flex;
              height: 67px;
              margin-bottom: 8px;
              justify-content: space-between;
              img {
                width: 80px;
                height: 60px;
                border-radius: 6px;
              }
              .course-info-block {
                display: flex;
                flex-direction: column;
                width: 224px;
                height: 67px;
                border-bottom: 1px solid rgba(43, 51, 59, 0.1);
                .course-name {
                  padding-top: 8px;
                  font-size: 14px;
                  height: 22px;
                  line-height: 22px;
                  margin-bottom: 4px;
                  cursor: pointer;
                  color: #545c63;
                  overflow: hidden;
                  /*文本不会换行*/
                  white-space: nowrap;
                  /*当文本溢出包含元素时，以省略号表示超出的文本*/
                  text-overflow: ellipsis;
                }
                .value-applay {
                  font-size: 12px;
                  color: #545c63;
                  display: flex;
                  span {
                    margin-right: 10px;
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

::v-deep .el-dialog {
  width: 560px;
  border-radius: 10px;
  margin: 0 !important;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  .el-dialog__header {
    font-size: 16px;
    font-weight: bold;
    color: #1c1f21;
  }
  .el-dialog__body {
    padding-top: 10px;
    padding-bottom: 20px;
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    .el-textarea {
      margin-bottom: 20px;
    }
    .button-group {
      display: flex;
      justify-content: space-between;
      align-items: center;
      width: 100%;
      .my-rate {
        display: flex;
        align-items: center;
      }
      .el-button {
        background-color: $primary-color;
        color: white;
        border-radius: 20px;
      }
    }
  }
}

.width-container {
  width: 1152px;
  margin: 0 auto;
}
</style>